package com.tb.controller;

import com.github.pagehelper.PageInfo;
import com.tb.model.Answer;
import com.tb.model.Message;
import com.tb.service.AnswerService;
import com.tb.service.MessageService;
import com.tb.util.MyConstant;
import com.tb.util.MyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @description: 问题的答案相关
 * @author: wangguichun
 * @create: 2018-04-15 00:56
 **/

@Controller
@RequestMapping("answer")
public class AnswerController {

    @Autowired
    private AnswerService answerService;
    @Autowired
    private MessageService messageService;

    @RequestMapping("add")
    @ResponseBody
    public void add(Answer answer, int questionUserId) {
        answerService.addAnswer(answer);
        Message message = new Message();
        message.setFromId(answer.getUserId());
        message.setToId(questionUserId);
        message.setType(MyConstant.ACT_ANSWER);
        message.setObjecId(answer.getQuestionId());
        message.setTime(new Date().getTime());
        messageService.addMessage(message);
    }


    //我的回答
    @RequestMapping("myAns/{pageId}")
    public String myAnsList(@PathVariable Integer pageId, Model model, HttpServletRequest request) {
        int fromUserId = Integer.parseInt(MyUtils.getCookie(request, "id").getValue());
        PageInfo<Answer> pageInfo = answerService.getAnswerListByUserIdAndPageId(fromUserId, pageId);
//        PageBean<Answer> pageBean = new PageBean<>();
//        pageBean.setCurPage(pageId);
//        int pageCount = answerService.getCountOfAnswerByUserId(fromUserId) / MyConstant.PAGE_SIZE_ANSWER + 1;
//        pageBean.setAllPage(pageCount);
//        pageBean.setList(answerList);
//        model.addAttribute("pageBean", pageBean);
        model.addAttribute("pageInfo", pageInfo);
        return "answer";
    }

}
